#ifndef MODEL_H
#define MODEL_H

#include <QEventLoop>
#include <QProcess>
#include <QString>
#include "tcpclient.h"

class Model : public QObject
{
    Q_OBJECT
public:
    explicit Model(QObject* parent = nullptr);
    ~Model();
    void setModelInfo(
        const QString& modelNameOrServerURL, const QString& jsonTemplate, const QString& keys
    );
    void setQuestion(const QString& question, const QString& recordStr);
    void work();
    void initProcess();
    void freeProcess();
    bool initTcpClient();
    void freeTcpClient();
    void initEventLoop();
    void freeEventLoop();
    
    QString pickValues(const QByteArray& json);
    
signals:
    void signal_updateOutputArea(const QString& originalQuestion, const QString& answer);
    void signal_finished(const QString& question, const QString& answer);
private:
    QProcess* process = nullptr;
    
    TcpClient* tcpClient = nullptr;
    QEventLoop* loop = nullptr;
    
    QString currentModel;
    QString question;
    QString originalQuestion;
    QString answer;
    QString jsonTemplate;
    QString keys;
    
    QByteArray currentJsonContent;
    
    void slot_readFromQProcess();
    void slot_readFromTcpClient(const QByteArray& partialResponse);
    void slot_readFinalFromTcpClient(const QByteArray& partialResponse);
};

#endif // MODELINFO_H
